package com.bdmc.web.controller.car;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bdmc.car.domain.bo.CarGarageBo;
import com.bdmc.car.domain.vo.CarGarageVo;
import com.bdmc.car.service.ICarGarageService;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 修车厂控制器
 *
 * @author bdmc
 * @date 2023-01-06
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/car/garage")
public class CarGarageController extends BaseController {

    private final ICarGarageService iCarGarageService;

    /**
     * 查询修车厂列表
     */
    @SaCheckPermission("car:garage:list")
    @GetMapping("/list")
    public TableDataInfo<CarGarageVo> list(@Validated(QueryGroup.class) CarGarageBo bo, PageQuery pageQuery) {
        return iCarGarageService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出修车厂列表
     */
    @SaCheckPermission("car:garage:export")
    @Log(title = "修车厂", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated CarGarageBo bo, HttpServletResponse response) {
        List<CarGarageVo> list = iCarGarageService.queryList(bo);
        ExcelUtil.exportExcel(list, "修车厂", CarGarageVo.class, response);
    }

    /**
     * 获取修车厂详细信息
     */
    @SaCheckPermission("car:garage:query")
    @GetMapping("/{garageId}")
    public AjaxResult<CarGarageVo> getInfo(@PathVariable("garageId") Integer garageId) {
        return AjaxResult.success(iCarGarageService.queryById(garageId));
    }

    /**
     * 新增修车厂
     */
    @SaCheckPermission("car:garage:add")
    @Log(title = "修车厂", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody CarGarageBo bo) {
        return toAjax(iCarGarageService.insertByBo(bo));
    }

    /**
     * 修改修车厂
     */
    @SaCheckPermission("car:garage:edit")
    @Log(title = "修车厂", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody CarGarageBo bo) {
        return toAjax(iCarGarageService.updateByBo(bo));
    }

    /**
     * 删除修车厂
     */
    @SaCheckPermission("car:garage:remove")
    @Log(title = "修车厂" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{garageIds}")
    public AjaxResult<Void> remove(@PathVariable Integer[] garageIds) {
        return toAjax(iCarGarageService.deleteWithValidByIds(Arrays.asList(garageIds), true));
    }

    @GetMapping("combo")
    public AjaxResult<List<CarGarageVo>> combo(CarGarageBo bo){
        return AjaxResult.success(iCarGarageService.queryList(bo));
    }
}
